package zzf;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class TestResponseBody {


    @ResponseBody//将返回的数据放在响应体,如果是对象,自动转换为接送数据
    @RequestMapping("/testResponseBody")
    public Employee testResponseBody(){
        Employee employee = new Employee();
        employee.setLastName("小明");
        employee.setEmail("xiaoming@qq.com");
        return employee;
    }

    /*
    * @RequestBody:获取请求体
    * */
    @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String body){
        System.out.println("请求体:"+body);
//        请求体:------WebKitFormBoundaryGATTLxudABtOmSNT
//        Content-Disposition: form-data; name="username"
//
//        tomcat
//                ------WebKitFormBoundaryGATTLxudABtOmSNT
//        Content-Disposition: form-data; name="password"
//
//        123456
//                ------WebKitFormBoundaryGATTLxudABtOmSNT
//        Content-Disposition: form-data; name="file"; filename=""
//        Content-Type: application/octet-stream
        return "success";
    }


    /*
    * HttpEntity:获取请求头
    * */
    @RequestMapping("/testHttpEntity")
    public String testHttpEntity(HttpEntity<String> entity){
        System.out.println("请求头:"+entity);
//        请求头:<[host:"localhost:8080", connection:"keep-alive", upgrade-insecure-requests:"1", user-agent:"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36 Edg/83.0.478.37", accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", sec-fetch-site:"none", sec-fetch-mode:"navigate", sec-fetch-dest:"document", accept-encoding:"gzip, deflate, br", accept-language:"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6", cookie:"JSESSIONID=E0508B9F43EA169F6F934B64EDD3CBCB; Idea-c1044685=2394970d-dd57-49bd-850c-94f3b125479b; _ga=GA1.1.1467250925.1585729158"]>
        return "success";
    }

    /*
    * ResponseEntity:返回一个自定义的响应
    *   其中包含响应体,响应头,响应状态值
    * */
    @RequestMapping("/testResponseEntity")
    public ResponseEntity<String> testResponseEntity(){
        HttpHeaders httpHeaders = new HttpHeaders();
        String body = "<h1>success</h1>";
        httpHeaders.add("Set-Cookie","username=hahaha");
        return new ResponseEntity<String>(body,httpHeaders, HttpStatus.OK);
    }
}
